package com.service.admin.impl;

import com.dao.LogDao;
import com.entity.ErrorLog;
import com.entity.LoginHistory;
import com.entity.RegisterHistory;
import com.entity.VisitHistory;
import com.service.admin.AdminLogService;
import com.utils.SqlSessionGetter;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 管理员关于日志的服务实现类
 *
 * 
 */
@Service
public class AdminLogServiceImpl implements AdminLogService {
  @Override
  public List<RegisterHistory> getRegisterHistory(int pageSize, int pageIndex) {
    List<RegisterHistory> history;
    SqlSession session = SqlSessionGetter.getSqlSession();
    LogDao dao = session.getMapper(LogDao.class);
    history = dao.selectRegisterHistory(pageSize * pageIndex, pageSize);
    session.close();
    return history;
  }

  @Override
  public List<VisitHistory> getVisitHistory(int pageSize, int pageIndex) {
    List<VisitHistory> history;
    SqlSession session = SqlSessionGetter.getSqlSession();
    LogDao dao = session.getMapper(LogDao.class);
    history = dao.selectVisitHistory(pageSize * pageIndex, pageSize);
    session.close();
    return history;
  }

  @Override
  public List<LoginHistory> getLoginHistory(int pageSize, int pageIndex) {
    List<LoginHistory> history;
    SqlSession session = SqlSessionGetter.getSqlSession();
    LogDao dao = session.getMapper(LogDao.class);
    history = dao.selectLoginHistory(pageSize * pageIndex, pageSize);
    session.close();
    return history;
  }

  @Override
  public List<ErrorLog> getErrorHistory(int pageSize, int pageIndex) {
    List<ErrorLog> history;
    SqlSession session = SqlSessionGetter.getSqlSession();
    LogDao dao = session.getMapper(LogDao.class);
    history = dao.selectErrorHistory(pageSize * pageIndex, pageSize);
    session.close();
    return history;
  }

  @Override
  public int setErrorVisited(int dailyId) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    LogDao dao = session.getMapper(LogDao.class);
    int row = dao.updateErrorLogStatus(dailyId);
    session.commit();
    session.close();
    return row;
  }

  /** 将每日或每小时Map记录转化为数组,period为day或hour */
  private int[] parsePerPeriodRecord(List<Map<String, Object>> record, String period) {
    System.out.println(record);
    int length = 31;
    int offset = 1;
    if ("hour".equals(period)) {
      length = 24;
      offset = 0;
    }
    int[] ret = new int[length];
    for (Map<String, Object> item : record) {
      ret[Integer.parseInt(item.get(period).toString()) - offset] =
          Integer.parseInt(item.get("count").toString());
    }
    return ret;
  }

  private int[] getPerDayCount(String tableName, String columnName, String yearMonth) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    LogDao dao = session.getMapper(LogDao.class);
    List<Map<String, Object>> record = dao.selectPerDayCount(tableName, columnName, yearMonth);
    int[] ret = parsePerPeriodRecord(record, "day");
    session.close();
    return ret;
  }

  private int[] getPerHourCount(
      String tableName, String columnName, String startDate, String endDate) {
    SqlSession session = SqlSessionGetter.getSqlSession();
    LogDao dao = session.getMapper(LogDao.class);
    List<Map<String, Object>> record =
        dao.selectPerHourCount(tableName, columnName, startDate, endDate);
    int[] ret = parsePerPeriodRecord(record, "hour");
    session.close();
    return ret;
  }

  @Override
  public int[] getPerDayVisit(String yearMonth) {
    return getPerDayCount(LogDao.WEB_VISIT_TABLE_NAME, LogDao.WEB_VISIT_TIME_COLUMN, yearMonth);
  }

  @Override
  public int[] getPerHourVisit(String startDate, String endDate) {
    return getPerHourCount(
        LogDao.WEB_VISIT_TABLE_NAME, LogDao.WEB_VISIT_TIME_COLUMN, startDate, endDate);
  }

  @Override
  public int[] getPerDayLogin(String yearMonth) {
    return getPerDayCount(LogDao.WEB_LOGIN_TABLE_NAME, LogDao.WEB_LOGIN_TIME_COLUMN, yearMonth);
  }

  @Override
  public int[] getPerDayRegister(String yearMonth) {
    return getPerDayCount(
        LogDao.WEB_REGISTER_TABLE_NAME, LogDao.WEB_REGISTER_TIME_COLUMN, yearMonth);
  }
}
